package com.gmail.dengtao.joe.commons.util;

import java.util.regex.Pattern;

/**
 * ValidateUtils is used to validate email, ip and etc.
 * @author <a href="mailto:joe.dengtao@gmail.com">DengTao</a>
 * @version 1.0
 * @since 1.0
 */
public class ValidateUtils {
 
	// Regex
    private static final String EMAIL_REGEX = "^\\s*?(.+)@(.+?)\\s*$";
    private static final String IPV4_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
    private static final String URL_REGEX = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
    private static final String COLOR_HEX_REGEX = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";

    // Patterns
    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
    private static final Pattern IPV4_PATTERN = Pattern.compile(IPV4_REGEX);
    private static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX);
    private static final Pattern COLOR_HEX_PATTERN = Pattern.compile(COLOR_HEX_REGEX);
    
	/**
	 * Validate email address.
	 * @param email email for validation
	 * @return true valid email, false invalid email
	 */
	public static boolean isEmail(final String email) {
		return EMAIL_PATTERN.matcher(email).matches();
	}
	
	/**
	 * Validate ip address.
	 * @param ip ip for validation
	 * @return true valid ip address, false invalid ip address
	 * @since 1.0
	 */
	public static boolean isIp(final String ip) {
		return IPV4_PATTERN.matcher(ip).matches();
	}
	
	/**
	 * Validates URLs.
	 * @param url url for validation
	 * @return true valid URL, false invalid URL
	 * @since 1.0
	 */
	public static boolean isUrl(final String url) {
		return URL_PATTERN.matcher(url).matches();
	}
	
	/**
	 * Validates color hex.
	 * @param color url for validation
	 * @return true valid color hex, false invalid color hex
	 * @since 1.0
	 */
	public static boolean isColorHex(final String color) {
		return COLOR_HEX_PATTERN.matcher(color).matches();
	}
}